package com.shilan.administrator.qintfm.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shilan.administrator.qintfm.R;
import com.shilan.administrator.qintfm.Utils.CountToOthers;
import com.shilan.administrator.qintfm.beans.PlaylistResult;

import java.util.List;

/**
 * Created by dllo on 16/1/26.
 */
public class RecommendListAdapter extends BaseAdapter{
    private Context context;
    private List<PlaylistResult.TracksEntity.ListEntity> listEntity;

    public RecommendListAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<PlaylistResult.TracksEntity.ListEntity> listEntity) {
        this.listEntity = listEntity;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return listEntity != null && listEntity.size() > 0 ? listEntity.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (listEntity != null && listEntity.size() > 0) {
            PlayViewHolder playHolder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context)
                        .inflate(R.layout.play_list_item, parent, false);
                playHolder = new PlayViewHolder(convertView);
                convertView.setTag(playHolder);
            } else {
                playHolder = (PlayViewHolder) convertView.getTag();
            }
            playHolder.titlePlAdapterTv.setText(listEntity.get(position).getTitle());
            Log.d("PlayListAdapter", listEntity.get(position).getTitle());
            playHolder.playNumPlAdapterTv.setText(CountToOthers.getCountToWan(listEntity.get(position).getPlaytimes()));
            playHolder.timePlAdapterTv.setText(CountToOthers.getCountToHour(listEntity.get(position).getDuration()));
            playHolder.commentPlAdapterTv.setText(listEntity.get(position).getComments() + "");
        }
        return convertView;

    }

    class PlayViewHolder {
        private TextView titlePlAdapterTv, playNumPlAdapterTv, timePlAdapterTv, commentPlAdapterTv;

        public PlayViewHolder(View view) {
            titlePlAdapterTv = (TextView) view.findViewById(R.id.tvTitle_playListAdapter);
            playNumPlAdapterTv = (TextView) view.findViewById(R.id.tvplayNum_playListAdapter);
            timePlAdapterTv = (TextView) view.findViewById(R.id.tvTime_playListAdapter);
            commentPlAdapterTv = (TextView) view.findViewById(R.id.tvComment_playListAdapter);
        }
    }

}
